package CalendarComponents;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import calendar.MainWindow;

import Managers.Appointment;
import Managers.CalendarManager;

public class PopupMenu
{
	JPopupMenu popup;
	JMenuItem day, week, month;
	DayView parent;
	public PopupMenu(DayView p) {
		parent = p;
		popup = new JPopupMenu();
		JMenuItem menu = new JMenuItem("Add Appointment");
		menu.addActionListener(new ModAp());
		popup.add(menu);
		JMenu change = new JMenu("Change View");
		day = new JMenuItem("Day View");
		week = new JMenuItem("Week View");
		month = new JMenuItem("Month View");
		ViewListener vl = new ViewListener();
		day.addActionListener(vl);
		week.addActionListener(vl);
		month.addActionListener(vl);
		change.add(day);
		change.add(week);
		change.add(month);
		popup.add(change);
	}
		
	public JPopupMenu getMenu()
	{
		return popup;
	}
	
	private class ViewListener implements ActionListener {
    	public void actionPerformed(ActionEvent e)
    	{
    		Calendar c = parent.getDay().getCalendar();
    		
    		CalendarManager.getInstance().setNow(c);
    		
			if(e.getSource().equals(day))
			{
				MainWindow.getTabPane().setSelectedIndex(0);
			}
			else if(e.getSource().equals(week))
			{
				MainWindow.getTabPane().setSelectedIndex(1);
			}
			else if(e.getSource().equals(month))
			{
				MainWindow.getTabPane().setSelectedIndex(2);
				MainWindow.getMonthsTabPane().setSelectedIndex(c.get(Calendar.MONTH));
			}	
		}
    }
	
	private class ModAp implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//create Properties dialog
			PropertiesDialog pd = new PropertiesDialog(MainWindow.getMainWindow(), parent.getDay());
			
			//show it
			pd.setVisible(true);
			//get the appointment
			Appointment er = pd.getAppointment();
						
			if(er == null)
			{
				return;//most likly a close
			}
			else
			{	
				
				CalendarManager.AddAppointment(parent.getDay(), er);
			}
			
		}
	}
}
